﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Common;
using StoragePeriod.Data;

namespace StoragePeriod.Web.Nutritionknowledge
{
    public partial class Nutrientsintro : SecurityPage
    {
        public override string PrivCode()
        {
            return "9a9f6d9a1be2b151";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView("");
            }
        }

        public void BindTreeView(string currentvalue)
        {
            bool isFirst = string.IsNullOrEmpty(currentvalue) ? true : false; //是否默认
            bool haveFirst = false; //是否可以有默认
            int openNodeIndex = 0;//默认展开项
            int currentNode = 0;//当前项
            NutrientsTree.Nodes.Clear();

            NutritionType.All().ToList().ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.nutrition,
                    Value = p.id.EToString(),
                    Target = "Parent"
                };
                List<IntroduceNutrition> introlist = IntroduceNutrition.Where("typeId == @0", p.id).ToList();
                introlist.ForEach(a =>
                {
                    childNode.ChildNodes.Add(new TreeNode()
                    {
                        Text = a.title,
                        Value = a.id.EToString(),
                        Target = "Child",
                        Selected = currentvalue == a.id.ToString() ? true : false
                    });
                    if (currentvalue == a.id.ToString()) openNodeIndex = currentNode;
                });
                if (!NutrientsTree.Nodes.Contains(childNode)) NutrientsTree.Nodes.Add(childNode);
                if (introlist.Count > 0) haveFirst = true;
                currentNode++;
            });

            if (isFirst && haveFirst) NutrientsTree.Nodes[0].ChildNodes[0].Selected = true;
            NutrientsTree.Nodes[openNodeIndex].Expand();
            SysTree_SelectedNodeChanged(null, null);
        }

        protected void SysTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode tn = this.NutrientsTree.SelectedNode;
            if (tn!=null && tn.Target == "Child")
            {
                List<IntroduceNutrition> introlist = IntroduceNutrition.Where("id == @0", Convert.ToInt32(tn.Value)).ToList();
                introlist.ForEach(a =>
                {
                    this.HidID.Value = a.id.ToString();
                    this.IntroTitle.InnerText = a.title;
                    this.IntroContent.InnerHtml = a.cText;
                });
            }
        }

        protected void btn_Del_Click(object sender, EventArgs e)
        {
            TreeNode tn = this.NutrientsTree.SelectedNode;
            int id = 0;
            Int32.TryParse(tn.Value, out id);
            if (tn.Target == "Child")
            {
                IntroduceNutrition.Delete("id == @0", id);
            }
            else
            {
                NutritionType.Delete("id == @0", id);
            }

            string script = "<script type='text/javascript'>alert('删除成功！');</script>";
            ScriptManager.RegisterStartupScript(this, Page.GetType(), "", script, false);
            BindTreeView("");
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Txt_Search.Text))
            {
                return;
            }
            for (int i = 0; i < NutrientsTree.Nodes.Count; i++)
            {
                for (int ii = 0; ii < NutrientsTree.Nodes[i].ChildNodes.Count; ii++)
                {
                    if (NutrientsTree.Nodes[i].ChildNodes[ii].Text.Contains(Txt_Search.Text))
                    {
                        TreeNode node = NutrientsTree.Nodes[i].ChildNodes[ii];
                        node.Text = HttpUtility.HtmlDecode(node.Text).Replace(Txt_Search.Text, "<span class='Red'>" + Txt_Search.Text + "</span>");
                    }
                }
            }
        }
    }
}